class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Codec:

    def serialize(self, root):
        """Encodes a tree to a single string.
        
        :type root: TreeNode
        :rtype: str
        """
        if not root: return ''
        data = []
        queue = [root]
        while queue:
            newQueue = []
            for root in queue:
                if root:
                    newQueue.append(root.left)
                    newQueue.append(root.right)
                    data.append(str(root.val))
                else:
                    data.append(str(None))
            queue = newQueue
        return ','.join(data)

    def deserialize(self, data):
        """Decodes your encoded data to tree.
        
        :type data: str
        :rtype: TreeNode
        """
        if len(data)==0: return None
        data = [(None if d=='None' else int(d)) for d in data.split(',')]
        data.reverse()
        root = TreeNode(data[-1])
        data.pop()
        queue = [root]
        while queue:
            newQueue = []
            for r in queue:
                if data[-1]!=None:
                    r.left = TreeNode(data[-1])
                    newQueue.append(r.left)
                data.pop()
                if data[-1]!=None:
                    r.right = TreeNode(data[-1])
                    newQueue.append(r.right)
                data.pop()
            queue = newQueue
        return root

# Your Codec object will be instantiated and called as such:
root = TreeNode(1)
root.right = TreeNode(2)
codec = Codec()
print(codec.deserialize(codec.serialize(root)))